﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ESupervision.Models;

namespace ESupervision.Controllers
{
    public class ForumReplyController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: /ForumReply/
        public async Task<ActionResult> Index()
        {
            var forumreplies = db.ForumReplies.Include(f => f.ForumReplyPoster).Include(f => f.ForumReplyTopic);
            return View(await forumreplies.ToListAsync());
        }

        // GET: /ForumReply/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumReply forumreply = await db.ForumReplies.FindAsync(id);
            if (forumreply == null)
            {
                return HttpNotFound();
            }
            return View(forumreply);
        }

        // GET: /ForumReply/Create
        public ActionResult Create()
        {
            ViewBag.ForumReplyPosterID = new SelectList(db.Students, "Id", "UserName");
            ViewBag.ForumTopicID = new SelectList(db.ForumTopics, "ForumTopicID", "ForumTopicTitle");
            return View();
        }

        // POST: /ForumReply/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include="ForumReplyID,ForumReplyContent,ForumReplyPostedTime,ForumTopicID,ForumReplyPosterID")] ForumReply forumreply)
        {
            if (ModelState.IsValid)
            {
                db.ForumReplies.Add(forumreply);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ForumReplyPosterID = new SelectList(db.Students, "Id", "UserName", forumreply.ForumReplyPosterID);
            ViewBag.ForumTopicID = new SelectList(db.ForumTopics, "ForumTopicID", "ForumTopicTitle", forumreply.ForumTopicID);
            return View(forumreply);
        }

        // GET: /ForumReply/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumReply forumreply = await db.ForumReplies.FindAsync(id);
            if (forumreply == null)
            {
                return HttpNotFound();
            }
            ViewBag.ForumReplyPosterID = new SelectList(db.Students, "Id", "UserName", forumreply.ForumReplyPosterID);
            ViewBag.ForumTopicID = new SelectList(db.ForumTopics, "ForumTopicID", "ForumTopicTitle", forumreply.ForumTopicID);
            return View(forumreply);
        }

        // POST: /ForumReply/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include="ForumReplyID,ForumReplyContent,ForumReplyPostedTime,ForumTopicID,ForumReplyPosterID")] ForumReply forumreply)
        {
            if (ModelState.IsValid)
            {
                db.Entry(forumreply).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ForumReplyPosterID = new SelectList(db.Students, "Id", "UserName", forumreply.ForumReplyPosterID);
            ViewBag.ForumTopicID = new SelectList(db.ForumTopics, "ForumTopicID", "ForumTopicTitle", forumreply.ForumTopicID);
            return View(forumreply);
        }

        // GET: /ForumReply/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ForumReply forumreply = await db.ForumReplies.FindAsync(id);
            if (forumreply == null)
            {
                return HttpNotFound();
            }
            return View(forumreply);
        }

        // POST: /ForumReply/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            ForumReply forumreply = await db.ForumReplies.FindAsync(id);
            db.ForumReplies.Remove(forumreply);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
